package com.ygo.feihua.fragment;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.support.v4.app.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import cn.bmob.v3.datatype.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.util.*;
import com.yuyh.library.imgsel.*;
import java.io.*;

import com.ygo.feihua.LogInListener;
import com.ygo.feihua.R;
import android.view.animation.*;

public class FragmentMainLeft extends Fragment implements OnClickListener,LogInListener
{

	
	private LinearLayout yh_zl;
	//用户头像
	private ImageView yh_avatar;
	//用户名,签名
	private TextView yh_name,yh_qm;
	private Button yh_logout;
	private UserManagement um;
	
	private DialogUtils du;
	private Gongju gj;

	public void replaceAvatar(String avatarPath)
	{
		du.dialogj1(false, "", "正在替换中,请稍等");
		final BmobFile bmobFile = new BmobFile(new File(avatarPath));
		bmobFile.uploadblock(new UploadFileListener() {

				@Override
				public void done(BmobException p1)
				{
					if (p1 == null){
						MyUser myu1=new MyUser();
						myu1.setavatar(bmobFile);
						myu1.update(um.getUser().getObjectId(), new UpdateListener() {

								@Override
								public void done(BmobException p1){
									if (p1 == null){
										um.sxUser();
										gj.setTouxiang(yh_avatar, um.getUser());
										gj.show("替换成功");
										du.dis();				 
									}else{
										gj.toastBmobException(yh_avatar,"替换失败",p1);
										du.dis();
									}
									// TODO: Implement this method
								}
							});
					}else{
						gj.toastBmobException(yh_avatar,"上传失败",p1);
						du.dis();
					}
					// TODO: Implement this method
				}

				@Override
				public void onProgress(Integer value)
				{
					// 返回的上传进度（百分比）
				}
			});
		// TODO: Implement this method
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_main_left,null);
		
		initView(v);
		// TODO: Implement this method
		return v;
	}

	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.yh_avatar:
				if(um.getUser()!=null){
				ISNav.getInstance().toListActivity(getActivity(), Gongju.getPicConfig(1), CeHua.AVATAR_PATH_RESULT);
				}else{
					du.dialoglogin();
				}
				break;
			case R.id.yh_qm:
				modifySignature();
				break;
			case R.id.yh_logout:
				um.logOutUser();
				break;
		}
		// TODO: Implement this method
	}

	@Override
	public void login(MyUser mu)
	{
		sxUserInformation();
		yh_logout.setVisibility(View.VISIBLE);
		yh_logout.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.in_from_down));
		
		// TODO: Implement this method
	}

	@Override
	public void logout(){
		yh_logout.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.out_from_top));
		yh_logout.setVisibility(View.GONE);
		yh_qm.setVisibility(View.GONE);
		yh_name.setText("点击登录");	
		yh_avatar.setImageResource(R.drawable.jianbian);			
	}
	
	
	// 保存到sdcard
    public  String savePic(Bitmap b)
	{

        FileOutputStream fos = null;
        try
		{
            File f = new File(gj.PATH_GEN + "ygo助手/image/touxiang/" , "tx_"+um.getUser().getUsername()+".png");
            if (f.exists())
			{
                f.delete();
            }

            fos = new FileOutputStream(f);
            if (null != fos)
			{
                b.compress(Bitmap.CompressFormat.PNG, 100, fos);

                fos.flush();
                fos.close();
                return gj.PATH_GEN + "ygo助手/image/touxiang/"+"tx_"+um.getUser().getUsername()+".png";
            }
        }
		catch (FileNotFoundException e)
		{
            e.printStackTrace();
        }
		catch (IOException e)
		{
            e.printStackTrace();
        }
        return null;
    }


	
	
	//将进行剪裁后的图片显示到UI界面上 
	public void setPicToView(Intent picdata)
	{

		Bundle bundle = picdata.getExtras();
		if (bundle != null)
		{
			du.dialogj1(false, "", "正在替换中,请稍等");
			Bitmap photo = bundle.getParcelable("data");

			String pp1=  savePic(photo);

			final BmobFile bmobFile = new BmobFile(new File(pp1));
			bmobFile.uploadblock(new UploadFileListener() {

					@Override
					public void done(BmobException p1)
					{
						if (p1 == null)
						{
							MyUser myu1=new MyUser();
							myu1.setavatar(bmobFile);
							myu1.update(um.getUser().getObjectId(), new UpdateListener() {

									@Override
									public void done(BmobException p1)
									{
										if (p1 == null)
										{
											um.sxUser();
											//   Glide.with(getActivity()).load(Uri.parse(bmobFile.getFileUrl(getActivity()))).dontAnimate().placeholder(R.drawable.sc_tb).into(wo_tx);             
											gj.setTouxiang(yh_avatar, um.getUser());

											gj.show("替换成功");
											du.dis();
										}else{
											gj.toastBmobException(yh_avatar,"替换失败",p1);

											//gj.show("替换失败" + p1.getMessage());
											du.dis();
										}
										// TODO: Implement this method
									}


								});
						}else{
							gj.toastBmobException(yh_avatar,"上传失败",p1);

							//gj.show("上传失败" + p1.getMessage());
							du.dis();
						}
						// TODO: Implement this method
					}



					@Override
					public void onProgress(Integer value)
					{
						// 返回的上传进度（百分比）
					}
				});

		}
	}
	

	private void modifySignature(){
		View[] v=du.dialoge(false, "", "请输入签名");
		Button qd=(Button) v[1];
		final EditText ed=(EditText) v[0];
		ed.setSingleLine(true);
		ed.setText(yh_qm.getText().toString().trim());
		qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String s=ed.getText().toString().trim();
					if (s.length() > 50){
						gj.show("签名不能超过50字");
					}else{
						if (s.equals("")){
							gj.show("签名不能为空");
						}else{

							um.getUser().setsignature(s);
							um.getUser().update(um.getUser().getObjectId(), new UpdateListener(){

									@Override
									public void done(BmobException p1)
									{
										if (p1 == null)
										{
											um.sxUser();
											gj.show("修改成功");
											du.dis();
											yh_qm.setText(um.getUser().getsignature());

										}else{
											gj.toastBmobException(yh_qm,"修改签名失败",p1);
											du.dis();
										}
										// TODO: Implement this method
									}
								});
						}
						// TODO: Implement this method
					}
				}
			});
	}
	
	private void initView(View v)
	{
		yh_zl=(LinearLayout) v.findViewById(R.id.yh_zl);
		yh_name=(TextView) v.findViewById(R.id.yh_name);
		yh_qm=(TextView) v.findViewById(R.id.yh_qm);
		yh_avatar=(ImageView) v.findViewById(R.id.yh_avatar);
		yh_logout=(Button) v.findViewById(R.id.yh_logout);
		
		um=UserManagement.getDx();
		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		
		um.addLogInLostener(this);
		
		yh_qm.setOnClickListener(this);
		yh_avatar.setOnClickListener(this);
		yh_logout.setOnClickListener(this);
		if(um.getUser()!=null){
			yh_qm.setVisibility(View.VISIBLE);
			yh_name.setText(um.getUser().getUsername());
			if(um.getUser().getsignature()!=null){
				yh_qm.setText(um.getUser().getsignature());
			}else{
				yh_qm.setText("因为有你我才得以坚持下去");
			}
			gj.tuxian(um.getUser().getavatar(),yh_avatar);	
		}else{
			yh_qm.setVisibility(View.GONE);
			yh_name.setText("点击登录");	
			yh_logout.setVisibility(View.GONE);
		}
		
		
		// TODO: Implement this method
	}
	
	
	private void sxUserInformation(){
			gj.setTouxiang(yh_avatar, um.getUser());
		yh_name.setText(um.getUser().getUsername());
		yh_qm.setVisibility(View.VISIBLE);
		if (um.getUser().getsignature() != null){
			yh_qm.setText(um.getUser().getsignature());
		}else{
			yh_qm.setText("因为有你我才得以坚持下去");
		}
	}
	
	
}
